<template>
  <div>
    <el-dialog
      :visible.sync="addDialogVisible"
      title="添加角色"
      width="28%"
      @close="closeEvent"
    >
      <el-form ref="formRef" :model="addObj" label-width="100px" :rules="rules">
        <el-form-item label="角色标签" prop="name">
          <el-input v-model="addObj.name"></el-input>
        </el-form-item>
        <el-form-item label="说明" prop="remarks">
          <el-input v-model="addObj.remarks"></el-input>
        </el-form-item>
        <el-form-item label="权级" prop="weight" controls-position="right">
          <el-input-number
            :min="1"
            :max="99"
            v-model="addObj.weight"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary" @click="addEvent">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
//导入这个函数会导致 警告
import { addRole } from "../../Api/rightsmanage/index";
export default {
  name: "RoleComponent",
  props: {
    addDialog: {
      type: Boolean,
      defaultf: false,
    },
  },
  data() {
    return {
      addObj: {
        name: "",
        remarks: "",
        weight: 5,
      },
      rules: {
        name: [
          {
            required: true,
            message: `请输入角色名称`,
            trigger: "blur",
          },
        ],
        remarks: [
          {
            required: true,
            message: `请输入角色说明`,
            trigger: "blur",
          },
        ],
        weight: [
          {
            required: true,
            message: `请输入角色权力级(1-99)`,
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    addDialogVisible: {
      get() {
        return this.addDialog;
      },
      set() {
        console.log(this);
      },
    },
  },
  methods: {
    closeEvent() {
      this.$refs.formRef.resetFields();
      this.addDialogVisible = false;
      this.$emit("closeAdd");
    },

    async addEvent() {
      console.log(this.addObj);
      const { data: res } = await addRole(this.addObj);
      console.log(res);
      if (res.statusCode === 20000) {
        this.$message.success("添加角色成功");
        this.$emit('closeAdd');
      }
    },
  },
};
</script>

<style>
</style>